﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spotless.Data;
using Spotless.Data.DataAccess.Interfaces;

namespace Spotless.Services
{
    public class CustomerService
    {
        public CustomerService()
            : this(null)
        {
        }

        public CustomerService(IQueryRepository queryRepository)
        {
            this.QueryRepository = queryRepository ?? new Spotless.Data.DataAccess.Sql.QueryRepository();
        }

        protected virtual IQueryRepository QueryRepository { get; private set; }

        
        //public List<Customer> GetAllCustomers()
        //{
        //    return QueryRepository.Customer_GetAll().ToList();
        //}


        public IQueryable<Customer> GetAllCustomers(Dictionary<FilterExpression<Customer>, string> filters)
        {
            var tmp = QueryRepository.Customer_GetAll();
            if (filters != null)
            {
                foreach (var filter in filters)
                {
                    tmp = filter.Key.Filter(tmp, filter.Value);
                }
            }
            return tmp;
        }

        public PagedList<Customer> GetAllCustomers(Dictionary<FilterExpression<Customer>, string> filters, string sort, int page, int pageSize)
        {
            var tmp = GetAllCustomers(filters);
            if (sort != null)
            {
                var sortExpression = SortExpression<Customer>.Parse(sort);
                tmp = sortExpression.Sort(tmp);
            }
            return tmp.ToPagedList(page, pageSize);
        }

        
        /*public PagedList<Customer> GetAllCustomers(string sort, int page, int pageSize)
        {

            //var sortExpression = SortExpression<Customer>.Parse(sort);
            var tmp = QueryRepository.Customer_GetAll();

            if (sort != null)
            {
                var sortExpression = SortExpression<Customer>.Parse(sort);
                tmp = sortExpression.Sort(tmp);
            }
            return tmp.ToPagedList(page, pageSize);
        }*/

        public Customer CreateCustomer()
        {
            var customer = new Customer();
            QueryRepository.AddCustomer(customer);
            return customer;
        }

        public Customer GetCustomerGetById(int id)
        {
            return QueryRepository.Customer_GetById(id);
            //  Spotless.Data.Query Query_GetById(int id);
        }
        
        //category
        public List<Category> GetCategories()
        {
            return QueryRepository.Category_GetAll().ToList();
        }

        public Customer GetById(int id)
        {
            return QueryRepository.Customer_GetById(id);
        }


        public void SaveChanges()
        {
            QueryRepository.SaveChanges();
        }


   



        public Category GetCategoryById(int categoryId)
        {
            return QueryRepository.Category_GetById(categoryId);
        }

        public List<DrillDown> GetTopLevelDrillDown(int categoryId, byte typeId)
        {
            return QueryRepository.DrillDown_GetTopLevelByCategoryId(categoryId, typeId).ToList();
        }

        public List<DrillDown> GetTopLevelDrillDown(int categoryId, DrillDown.DrillDownTypes type)
        {
            return QueryRepository.DrillDown_GetTopLevelByCategoryId(categoryId, (byte)type).ToList();
        }

        public List<DrillDown> GetDrillDownsByParentId(int id)
        {
            return QueryRepository.DrillDown_GetAllByParentId(id).ToList();
        }

        public List<DrillDown> GetDrillDownsByParentId(int id, DrillDown.DrillDownTypes type)
        {
            return QueryRepository.DrillDown_GetAllByParentId(id, (byte)type).ToList();
        }

        public DrillDown GetDrillDownById(int id)
        {
            return QueryRepository.DrillDown_GetById(id);
        }

    }
}
